class scriptmassage
{
	target:COBJECT;
	script:string;
}



class ASSIST {
	private child:CBASE
	private stage:egret.Stage
    private static myself:ASSIST

	private Weightpos:egret.Point
	private finalpos:egret.Point
	public speed:number=1
	public camerafollow:boolean=false;
	private Yautosort:boolean=false;
	private childbeginpos:egret.Point
	public platte:any
	private plattebeginpos:egret.Point;

	private collition:CCOLLISION[]=[];
	private SCRIPT:scriptmassage[]=[];

	private backgroundnumber:number=1; 
	public get player():CBASE
	{
		return this.child
	}
	private constructor() {
	
		this.stage=egret.MainContext.instance.stage 
		TIMERUP.gettimercontrl.addTouchEvent(this,"automove")
		TIMERUP.gettimercontrl.addFrameEvent(this,"onmove")
        this.finalpos=new egret.Point(0,0);
		this.Weightpos=new egret.Point(0,0);
		this.plattebeginpos=new egret.Point(0,0);
		this.childbeginpos=new egret.Point(0,0);
	}
	public get bodydata():CCOLLISION[]
	{
		return this.collition;
	}
	public get scriptdata():scriptmassage[]
	{
		return this.SCRIPT;
	}
	public set Yautosor(a:boolean)
	{
		this.Yautosort=a
		if(a)
		{
			TIMERUP.gettimercontrl.addFrameEvent(this,"Ysort")
			
		}
		else 
		{
			TIMERUP.gettimercontrl.removeframeevent(this,"Ysort")
		}
	}
	public get Yautosor():boolean{
		return this.Yautosort;
	}
	public pushinscript(target:COBJECT,scriptclassname:string)
	{
		let temp=new scriptmassage;
		temp.script=scriptclassname;
		temp.target=target;
		this.SCRIPT.push(temp);
	}
	public removescript(target:COBJECT,scriptclassname:string)
	{
      for(let a=0;a<this.SCRIPT.length;a++)
	  {
		  if(this.SCRIPT[a].target==target&&this.SCRIPT[a].script==scriptclassname)
		  {
			  this.SCRIPT.splice(a,1);
			  a--;
		  }
	  }
	}
	public getscriptlist(target:COBJECT):string[]	
	{
		let scriptname:string[]=[]
          for(let a=0;a<this.SCRIPT.length;a++)
	     {
		  if(this.SCRIPT[a].target==target)
		  {
			 scriptname.push(this.SCRIPT[a].script)
		  }
	     }
		 return scriptname
	}
	public removeALLscript(target:COBJECT)
	{
		 for(let a=0;a<this.SCRIPT.length;a++)
	  {
		  if(this.SCRIPT[a].target==target)
		  {
			  this.SCRIPT.splice(a,1);
			  a--;
		  }
	  }
	}
	private Ysort()
	{
	   let number=this.platte.numChildren/2
       for(let a=1;a<this.collition.length;a++)
	   {
          let tem=this.collition[a]
		  let b=a-1;
		  while(b>-1&&tem.shape.localToGlobal().y<this.collition[b].shape.localToGlobal().y)
		  {
			  this.collition[b+1]=this.collition[b]
			  b--;
		  }
		  this.collition[b+1]=tem
	   }
	   for(let a=0;a<this.collition.length;a++)
	   {
		   if(this.platte.getChildIndex(this.collition[a].parent)!=a+this.backgroundnumber)
		      this.platte.setChildIndex(this.collition[a].parent,a+this.backgroundnumber);
	   }
	}
	public pushinbodycollsion(a:CCOLLISION)
	{
	
		this.collition.push(a);
	
	}
	public removeoutbodycollsion(co:CCOLLISION)
	{
		for(let a=0;a<this.collition.length;a++)
		{
			if(this.collition[a]==co)
			{
				this.collition.splice(a,1)
			}
		}
	}
	public checkbodycollision(co:CCOLLISION)
	{
		for(let a=0;a<this.collition.length;a++)
		{
			if(this.collition[a]==co)
			{
				return true;
			}
		}
		return false;
	}
	public setplatte(a:any)
	{
		this.platte=a;
	}
	public backgroundcount(a:boolean)
	{
		if(a)
		this.backgroundnumber++;
		else 
		this.backgroundnumber--;
	}
    
	public static get getASSIST():ASSIST
	{
		
        if(this.myself==undefined)this.myself=new ASSIST;
		return this.myself
	}
    public addautomvoeassist(sonme:CBASE)
	{
      this.child=sonme
	}
	public removeautomoveassist(some:CBASE)
	{
		if(this.child==some)
		this.child=undefined;
	}
	private automove(e:egret.TouchEvent)
	{
       if(this.child==undefined)return;
	   if(e.type==egret.TouchEvent.TOUCH_BEGIN||e.type==egret.TouchEvent.TOUCH_MOVE)
	   {
		    
		   this.finalpos.x=e.stageX;
		   this.finalpos.y=e.stageY;
           console.log(e.stageX,e.stageY,this.child.localToGlobal().x,this.child.localToGlobal().y)
		   this.Weightpos.x=this.finalpos.x-this.child.localToGlobal().x
		   this.Weightpos.y=this.finalpos.y-this.child.localToGlobal().y
		   let long=Math.sqrt(this.Weightpos.x*this.Weightpos.x+this.Weightpos.y*this.Weightpos.y)
		   this.Weightpos.x/=long
		   this.Weightpos.y/=long
	   }
	}
	public resetbeginpos()
	{
       this.child.x=this.childbeginpos.x
	   this.child.y=this.childbeginpos.y
	    if(this.camerafollow)
	   {
		 
	     this.platte.x+=this.plattebeginpos.x
	     this.platte.y+=this.plattebeginpos.y
	   }
	   this.STOP_ALLSCRIPT();
	  
	}
	public loadbeginpos()
	{
		this.childbeginpos.x=this.child.x;
		this.childbeginpos.y=this.child.y;
		this.plattebeginpos.x=0;
	    this.plattebeginpos.y=0;
		CBASE_MANAGE.getCBASE_MANAGE.ALLobjCollition();
	    this.loadScript();
		CBASE_MANAGE.getCBASE_MANAGE.Onstart();
	}
	private loadScript()
	{
       	for(let a=0;a<this.SCRIPT.length;a++)
		{
			var temp=egret.getDefinitionByName(this.SCRIPT[a].script);
		    var anyone=new temp;
			anyone.target=this.SCRIPT[a].target;
		}

	}
	private STOP_ALLSCRIPT()
	{
		CBASE_MANAGE.getCBASE_MANAGE.clearALLscript();
	}
	private onmove()
    {
		 if(this.child==undefined||((this.child.x==this.finalpos.x)&&(this.child.y==this.finalpos.y)))return;
		 else 
		 {
			 let x=this.Weightpos.x*this.speed
			 let y=this.Weightpos.y*this.speed
			 /*if(this.child.x+x>=0&&this.child.x+x+this.child.width-this.child.anchorOffsetX<=this.platte.width)*/
			 this.child.x+=x;
			/* else x=0;*/
			 /*if(this.child.y+y>=0&&this.child.y+y+this.child.height-this.child.anchorOffsetY<=this.platte.height)*/
			 this.child.y+=y;
			/** else y=0;*/
			 if(this.camerafollow)
			 {
			  this.plattebeginpos.x+=x
			  this.plattebeginpos.y+=y
			   this.platte.x-=x
	           this.platte.y-=y
			 }
			 let long=Math.sqrt(Math.pow((this.child.localToGlobal().x-this.finalpos.x),2)+Math.pow((this.child.localToGlobal().y-this.finalpos.y),2))
			 if(long<=this.speed)
			 {
				let finalpos=this.platte.globalToLocal(this.finalpos.x,this.finalpos.y)
				 this.child.x= finalpos.x
				 this.child.y=finalpos.y

			 }
		 }
	}
	public goback()
	{
		console.log("111111111111")
		 let x=this.Weightpos.x*this.speed
		let y=this.Weightpos.y*this.speed
		this.child.x-=x;
		this.child.y-=y;
		 if(this.camerafollow)
			 {
			  this.plattebeginpos.x-=x;
			  this.plattebeginpos.y-=y;
			   this.platte.x-=x;
	           this.platte.y-=y;
			 }
	}
	public clearALL()
	{
         	//先清空数据
		this.collition.splice(0,this.collition.length)
	    this.SCRIPT.splice(0,this.SCRIPT.length)
		this.backgroundnumber=1
		this.child=undefined;
	}
	public loadingByjson(bodydata:any,scriptdata:any)
	{
	
			console.log("assistloading",bodydata,scriptdata);
		for(let a=0;a<bodydata.length;a++)
		{
			let parent=CBASE_MANAGE.getCBASE_MANAGE.getanyoneByname(bodydata[a].parentname)
			let collition=parent.getjudgeByname(bodydata[a].bodyname)
		
			this.pushinbodycollsion(collition);
		}
		for(let b=0;b<scriptdata.length;b++)
		{
			
			let parent=CBASE_MANAGE.getCBASE_MANAGE.getanyoneByname(scriptdata[b].targetname)
			let scriptmassage=scriptdata[b].scriptname
			this.pushinscript(parent,scriptmassage);
		}
	}
}